<template>
  <div>
    <el-select v-model="selectValue" @change="selectChange" placeholder="请选择" style="margin: 10px 20px;">
      <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
    </el-select>
    <el-input placeholder="请输入内容" v-model="serachVal" class="input-with-select">
      <el-button slot="append" icon="el-icon-search" @click="handleSerach(serachVal)"></el-button>
    </el-input>
    <el-tree :data="treeNode" :props="treeProps"
             highlight-current
             @node-click="treeNodeClick">
      <span class="custom-tree-node" slot-scope="{ node }">
        <span>{{ node.label }}</span>
        <span>
          <el-dropdown>
            <span class="el-dropdown-link">
              <i class="el-icon-circle-plus-outline"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addPhysicalModel(node.id)">添加物理表</el-dropdown-item>
              <el-dropdown-item>添加关联表</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </span>
      </span>
    </el-tree>
  </div>
</template>
<script>
export default {
  //初始化数据
  data() {
    return {
      treeNode: [],
      treeData: [],
      treeProps: {
        children: "children",
        label: "name"
      },
      id: "-1",
      root: "",
      options: [],
      selectValue: "",
      serachVal: ""
    };
  },
  components: {},
  //方法
  methods: {
    //请求树方法
    loadNode() {
      let _this = this;
      this.axios
        .post(this.HOST + "model/domainTree", { id: _this.selectValue })
        .then(function(res) {
          if (!res.data.status) {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定"
            });
          } else {
            if (res.data.data == null) {
              _this.treeNode = [];
            } else {
              _this.treeNode = res.data.data;
            }
          }
        })
        .catch(function(err) {
          console.log(err);
        });
    },
    selectChange(data) {
      this.selectValue = data;
      this.loadNode();
    },

    //树节点点击事件
    treeNodeClick(data) {
      this.EventBus.$emit("refreshGrid",data);
    },
    //点击树右侧加号
    append(data) {
      console.log(data);
    },
    //搜索点击事件
    handleSerach(data) {
      console.log(data);
    },
    //添加物理模型
    addPhysicalModel(data) {
      this.$router.push({
        path: "/addModel",
        query: { id: data }
      });
    },
    //数据域
    getDataLayer() {
      let _this = this;
      this.axios
        .post(this.HOST + "model/findAllLayer")
        .then(function(res) {
          if (!res.data.status) {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定"
            });
          } else {
            _this.options = res.data.data;
            _this.selectValue = res.data.data[0].id || "";
            _this.loadNode();
          }
        })
        .catch(function(err) {
          console.log(err);
        });
    }
  },
  //页面渲染之前调用方法 函数
  created() {},
  //页面渲染完成调用方法 函数
  mounted() {
    this.getDataLayer();
  }
};
</script>
<style scoped>
.input-with-select {
  width: 208px;
  margin-bottom: 10px;
}
.el-tree {
  margin-left: 10px;
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>

